//
//  NSString+SCStringHandle.m
//  Shuaiche
//
//  Created by dangshuai on 2019/3/26.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "NSString+SCStringHandle.h"

@implementation NSString (SCStringHandle)

- (NSString *)substringToCharater:(NSString *)charater withLocation:(int)location {
    NSArray *array = [self componentsSeparatedByString:charater];
    if (array.count >= location) {
        NSArray *subArray  = [array subarrayWithRange:NSMakeRange(0, location)];
        return [subArray componentsJoinedByString:charater];
    } else {
        return self;
    }
//    int findout = 0;
//    NSString *sub = [self copy];
//    NSRange resultRange = NSMakeRange(0, 0);
//    do {
//        NSRange range = [sub rangeOfString:charater];
//        if (range.location == NSNotFound ||
//            range.length == 0) {
//            resultRange = NSMakeRange(0, 0);
//            break;
//        }
//        sub = [sub substringFromIndex:range.location + 1];
//        findout++;
//        resultRange.length = resultRange.length + range.location + range.length;
//
//    } while (findout < location);
//
//    if (resultRange.length > 0) {
//        resultRange.length--;
//        return [self substringWithRange:resultRange];
//    }
//    return nil;
}

NSString *stringYYYYMMDD(NSString *ori) {
    if (ori.length) {
        NSRange range = [ori rangeOfString:@"\\d{4}(-\\d{1,2}){0,2}" options:NSRegularExpressionSearch];
        if (range.location < NSNotFound) {
            return [ori substringWithRange:range];
        }
    }
    return ori;
}

- (NSArray *)arrayByRegularPattern:(NSString *)pattern {
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:kNilOptions error:nil];
    NSMutableArray *marray = [NSMutableArray arrayWithCapacity:0];
    NSString *string = [self copy];
    [regular enumerateMatchesInString:self options:kNilOptions range:NSMakeRange(0, self.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
        [marray addObject:[string substringWithRange:result.range]];
    }];
    return marray;
}

- (NSNumber *)translateToTenThousand {
    NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:self];
    NSNumber *price = [num decimalNumberByMultiplyingByPowerOf10:4];
    if (price.longValue > 0) {
        return price;
    }
    return nil;
}
- (BOOL) checkFloatNumber {
    NSString *pre = @"^[1-9]\\d{0,4}(\\.\\d{0,2})?|0(\\.[1-9]?\\d?)?|0(\\.0[1-9]{0,1})?$";
    NSPredicate *floatPredicate =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", pre];
    return [floatPredicate evaluateWithObject:self];
}
- (BOOL) checkPhone {
    NSString *phoneRegex = @"^\\d{11}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:self];
}

- (BOOL)checkVin {
    NSString *phoneRegex = @"^\\w{17}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:self];
}

+ (NSString *)dayStringFromTime:(long)time {
    int secondsInDay = 3600 * 24 *1000; //毫秒
    if (time < secondsInDay) {
        return @"今日";
    }
    return [NSString stringWithFormat:@"%ld日",time / secondsInDay];
}

- (CGFloat)heightInWidth:(CGFloat)width forFont:(UIFont *)font {
    CGRect bound = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return ceilf(CGRectGetHeight(bound));
}

@end
